﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using Common.DBOper;
using Common;
using SignalProcessLib;
using SignalProcessLib.Model;
using System.IO;

namespace VibrationTest.OffLineChart
{
    public partial class frmLineChart_offline : Form
    {
        public frmLineChart_offline()
        {
            InitializeComponent();
        }
        double[] x_value = null;
        double[] y_value = null;
        double[] x_value_all = null;
        double[] y_value_all = null;
        string tishi_x = "";
        double mindata = 0;
        double maxdata = 0;
        int chartType = 0;//0代表对数坐标，线性坐标
        double max_data = -1;
        double min_data = 0;
        Series start_ser = new Series();
        Series end_ser = new Series();
        public int minid = 0;
        public int maxid = 0;
        public double chart1_Max = 0.0;
        public double chart1_Min = 0.0;

        public string nowUnit = "加速度(dB)";

        OperateData operdata = new OperateData();
        OperateSection opersection = new OperateSection();
        OperateTest opertest = new OperateTest();

        Dictionary<string, testTodata> testNameToData = new Dictionary<string, testTodata>();
        Dictionary<string, Series> testNameToSeries = new Dictionary<string, Series>();
        Dictionary<string, Color> testNameToColor = new Dictionary<string, Color>();
        Series yb1 = new Series();
        Series yb2 = new Series();
        Series xzx = new Series();
        bool isaddyb = false;
        OperationText opertext = new OperationText();
        RmsCalculation rms = new RmsCalculation();
        UnitConverter unitconverter = new UnitConverter();
        public string chart_type = "";
        double[] axis_txt = new double[31] { 10, 12.5, 16, 20, 25, 31.5, 40, 50, 63, 80, 100, 125, 160, 200, 250, 315, 400, 500, 630, 800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300,8000,10000 };
        double[] xzzValue = new double[31];
        private void chart1_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
                {
                    DataGridViewSelectedRowCollection rowCollection = e.Data.GetData(typeof(DataGridViewSelectedRowCollection)) as DataGridViewSelectedRowCollection;
                    if (rowCollection == null)
                    {
                        return;
                    }
                    //新增行
                    //注意要将鼠标的Point转换到当前工作区域，否则无法得到正确的HitTestInfo
                    System.Drawing.Point p = this.chart1.PointToClient(new System.Drawing.Point(e.X, e.Y));

                    //如果鼠标所在的位置的RowIndex>-1，则在当前位置接入列，否则就在最末尾新增列
                    //Console.WriteLine(this.dataGridView1.Rows[indexOfItemUnderMouseToDrag].Cells[0].Value.ToString());
                    //Console.WriteLine(rowCollection[0].Cells[0].Value.ToString());
                    //DisplayChart(this.dataGridView1.Rows[indexOfItemUnderMouseToDrag].Cells[0].Value.ToString());
                    DisplayChart(rowCollection[0].Cells[0].Value.ToString(), rowCollection[0].Cells[1].Value.ToString(), rowCollection[0].Cells[2].Value.ToString(), rowCollection[0].Cells[3].Value.ToString(), rowCollection[0].Cells[4].Value.ToString(), rowCollection[0].Cells[5].Value.ToString());
                }
            }
            catch (Exception ex)
            {
                //throw (ex);
            }

        }

        string Nowchannlname = "";
        string testname = "";
        string nowsectionName = "";
        string nowtestName = "";
        string nowchannel = "";
        string nowChartName = "";
        OperRunSet operunset = new OperRunSet();
        Cls_RunSet runset = new Cls_RunSet();
        int chart_Type = 0;//1，对数坐标 0线性坐标
        string nowtestid = "";

        public void DisplayChart(string channelid, string testid, string channlname, string sectionname, string testname1, string channelname1)
        {
            if (chart_type != "")
            {
                if (!channlname.Contains("1/3倍频程"))
                {
                    MessageBox.Show("该图形只支持1/3倍频程！", "提示");
                    return;
                }
            }

            if (nowChartName != "" && nowtestid != "")
            {
                if (!channlname.Contains(nowChartName))
                {
                    MessageBox.Show("不支持不同数据类型同时显示", "提示");
                    return;
                }
            }
            testname = sectionname + "_" + testname1 + "_" + channlname;
            nowsectionName = sectionname;
            nowtestName = testname1;
            nowchannel = channelname1;
            nowtestid = testid;
            if (chart_type == "") {
                if (channlname.Contains("1/3倍频程"))
                {
                    MessageBox.Show("该图形不支持该数据类型！", "提示");
                    return;
                }
            }
            if (testNameToData.ContainsKey(channelid + testid + channlname))
            {
                MessageBox.Show("该通道的数据已经存在！", "提示");
                return;
            }
            Nowchannlname = channlname;
            runset = operunset.GetRunSet(testid);

            Console.WriteLine("通道Id:" + channelid + "  测试ID:" + testid + "  通道名称:" + channlname);
            //this.chart1.Series[0].Points.Clear();
            //double[] x = new Double[1000];
            //double[] y = new Double[1000];
            //Random random = new Random();
            //for (int i = 0; i < 1000; i++)
            //{
            //    int a = random.Next(50);
            //    x[i] = Convert.ToDouble(i + 1);
            //    y[i] = Convert.ToDouble(a);
            //}
            //this.chart1.Series[0].Name = channlname;
            //this.chart1.Series[0].Points.DataBindXY(x, y);

            Cls_Data clsdatalist = operdata.GetDataByChannelid(testid, Convert.ToByte(channelid));
            if (clsdatalist != null)
            {
                DisplayChart(clsdatalist);
            }
        }



        public double yb = 0.0;
        //显示游标值
        public void DispalyCursorPosition(bool IsDisplayXY)
        {
            try
            {


                Point _mousePoint = Control.MousePosition;
                tishi_x = this.chart1.ChartAreas[0].CursorX.Position.ToString();
                int _x = this.chart1.PointToClient(_mousePoint).X;
                int _y = this.chart1.PointToClient(_mousePoint).Y;
                if (isaddyb == false)
                {



                    string tishi_y = "";
                    try
                    {
                        if (tishi_x != "非数字" && tishi_x != null && tishi_x != "")
                        {
                            if (Nowchannlname.Contains("倍频程"))
                            {
                                tishi_x = this.chart1.ChartAreas[0].CursorX.Position.ToString();
                            }
                            else {
                                if (chart_Type == 1)
                                {
                                    tishi_x = (Convert.ToInt32(Math.Pow(10, this.chart1.ChartAreas[0].CursorX.Position) / Convert.ToDouble(runset.Resolution))).ToString();
                                }
                                else
                                {
                                    tishi_x = (Convert.ToInt32(this.chart1.ChartAreas[0].CursorX.Position / Convert.ToDouble(runset.Resolution))).ToString();
                                }

                            }
                        }
                        else
                        {
                            tishi_y = "非数字";
                        }



                        if (this.chart1.Name == "chart1")
                        {


                            string toolText = "";
                            for (int i = 0; i < this.chart1.Series.Count; i++)
                            {
                                if (!this.chart1.Series[i].LegendText.Contains("游标") && !this.chart1.Series[i].Name.Contains("游标"))
                                {

                                    tishi_y = this.chart1.Series[i].Points[Convert.ToInt32(tishi_x) - 1].YValues[0].ToString();


                                    if (Nowchannlname.Contains("倍频程"))
                                    {

                                        tishi_y = this.chart1.Series[i].Points[Convert.ToInt32(tishi_x)].YValues[0].ToString();
                                        toolText = toolText + this.chart1.Series[i].LegendText + " X轴:" + axis_txt[Convert.ToInt32(tishi_x)].ToString() + " Y轴:" + tishi_y + "\n";
                                    }
                                    else
                                    {
                                        toolText = toolText + this.chart1.Series[i].LegendText + " X轴:" + (Math.Round(Convert.ToDouble(tishi_x) * Convert.ToDouble(runset.Resolution), 2)).ToString() + " Y轴:" + tishi_y + "\n";
                                    }
                                }
                            }
                            if (IsDisplayXY)
                            {
                                this.toolTip1.Show(toolText, this.chart1, _x, _y);
                            }
                            else
                            {
                                this.lb_DYB_X_Y.Text = toolText;
                            }
                            Console.WriteLine("x,y"+_x.ToString()+","+_y.ToString());

                        }



                    }

                    catch (Exception ex)
                    {
                        tishi_y = "";
                        //this.toolTip1.RemoveAll();
                    }
                }
                else if (isaddyb && ismove)
                {
                    if (movSeries.Name == "双游标1")
                    {
                        double cz = 0;

                        //yb1.Points[0].XValue = this.chart1.ChartAreas[0].CursorX.Position;
                        //yb1.Points[1].XValue = this.chart1.ChartAreas[0].CursorX.Position;

                        //yb2.Points[0].XValue = this.chart1.ChartAreas[0].CursorX.Position + cz;
                        //yb2.Points[1].XValue = this.chart1.ChartAreas[0].CursorX.Position + cz;
                        //}
                        double Curx_value = 0;

                        if (chart_Type == 1)
                        {
                            if (Math.Pow(10, this.chart1.ChartAreas[0].CursorX.Position + cz) < this.chart1.ChartAreas[0].AxisX.Maximum)
                            {



                                cz = Math.Log10(yb2.Points[0].XValue) - Math.Log10(yb1.Points[0].XValue);

                                Curx_value = Math.Pow(10, this.chart1.ChartAreas[0].CursorX.Position);

                                yb1.Points[0].XValue = Curx_value;
                                yb1.Points[1].XValue = Curx_value;
                                yb2.Points[0].XValue = Math.Pow(10, this.chart1.ChartAreas[0].CursorX.Position + cz);
                                yb2.Points[1].XValue = Math.Pow(10, this.chart1.ChartAreas[0].CursorX.Position + cz);
                            }
                        }
                        else
                        {
                            if (yb2.Points[0].XValue + cz < this.chart1.ChartAreas[0].AxisX.Maximum)
                            {
                                cz = yb2.Points[0].XValue - yb1.Points[0].XValue;
                                Curx_value = this.chart1.ChartAreas[0].CursorX.Position;

                                yb1.Points[0].XValue = Curx_value;
                                yb1.Points[1].XValue = Curx_value;

                                yb2.Points[0].XValue = Curx_value + cz;
                                yb2.Points[1].XValue = Curx_value + cz;
                            }
                        }
                    }
                    else
                    {
                        //if (this.chart1.ChartAreas[0].CursorX.Position > yb1.Points[0].XValue)
                        //{           
                        if (chart_Type == 1)
                        {
                            double Curx_value = Math.Pow(10, this.chart1.ChartAreas[0].CursorX.Position);
                            yb2.Points[0].XValue = Curx_value;
                            yb2.Points[1].XValue = Curx_value;
                        }
                        else
                        {
                            double Curx_value = this.chart1.ChartAreas[0].CursorX.Position;

                            yb2.Points[0].XValue = Curx_value;
                            yb2.Points[1].XValue = Curx_value;
                        }
                        //}
                    }

                    string toolText = "";
                    if (IsDisplayXY)
                    {
                        this.toolTip1.Show(toolText, this.chart1, _x, _y);
                    }
                    else
                    {
                        this.lb_DYB_X_Y.Text = toolText;
                    }
                    Dictionary<string, CursorValue> chartNameToCursorValue = new Dictionary<string, CursorValue>();
                    for (int i = 0; i < this.chart1.Series.Count; i++)
                    {
                        if (!this.chart1.Series[i].LegendText.Contains("游标") && !this.chart1.Series[i].Name.Contains("游标"))
                        {
                            int y1_value = 0;
                            int y2_value = 0;
                            if (Nowchannlname.Contains("倍频程"))
                            {
                                y1_value = Convert.ToInt32(yb1.Points[0].XValue);
                                y2_value = Convert.ToInt32(yb2.Points[0].XValue);
                            }
                            else
                            {
                                if (chart_Type == 1)
                                {
                                    //y1_value = Convert.ToInt32((Convert.ToInt32(Math.Pow(10, yb1.Points[0].XValue) / Convert.ToDouble(runset.Resolution))));
                                    //y2_value = Convert.ToInt32((Convert.ToInt32(Math.Pow(10, yb2.Points[0].XValue) / Convert.ToDouble(runset.Resolution))));
                                    y1_value = Convert.ToInt32((Convert.ToInt32(yb1.Points[0].XValue / Convert.ToDouble(runset.Resolution))));
                                    y2_value = Convert.ToInt32((Convert.ToInt32(yb2.Points[0].XValue / Convert.ToDouble(runset.Resolution))));
                                }
                                else
                                {
                                    y1_value = Convert.ToInt32((Convert.ToInt32(yb1.Points[0].XValue / Convert.ToDouble(runset.Resolution))));
                                    y2_value = Convert.ToInt32((Convert.ToInt32(yb2.Points[0].XValue / Convert.ToDouble(runset.Resolution))));
                                }
                            }


                            CursorValue cv = GetCursorValue(this.chart1.Series[i], y1_value, y2_value, Math.Round(yb1.Points[0].XValue, 2), Math.Round(yb2.Points[0].XValue, 2));
                            toolText = toolText + this.chart1.Series[i].LegendText + " X左:" + Math.Round(yb1.Points[0].XValue, 2).ToString() + " X右:" + Math.Round(yb2.Points[0].XValue, 2).ToString()
                                + " 最大值:" + cv.Max_Value.ToString() + " 最小值:" + cv.Min_Value.ToString() + " 平均值:" + cv.Avg_Value.ToString() + "\n";
                            chartNameToCursorValue.Add(this.chart1.Series[i].LegendText, cv);


                        }
                    }

                    //this.toolTip1.Show(toolText, this.chart1, _x, _y);
                    for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                    {
                        string chartlegtxt = this.dataGridView1.Rows[i].Cells[0].Value.ToString();
                        if (chartNameToCursorValue.ContainsKey(chartlegtxt))
                        {
                            if (Nowchannlname.Contains("倍频程"))
                            {
                                this.dataGridView1.Rows[i].Cells[1].Value = axis_txt[Convert.ToInt32(chartNameToCursorValue[chartlegtxt].leftValue)] + "Hz";
                                this.dataGridView1.Rows[i].Cells[2].Value = axis_txt[Convert.ToInt32(chartNameToCursorValue[chartlegtxt].rightValue)] + "Hz";
                                if (isaddyb)
                                {
                                    this.txt_left_value.Text = axis_txt[Convert.ToInt32(chartNameToCursorValue[chartlegtxt].leftValue)].ToString();
                                    this.txt_right_value.Text = axis_txt[Convert.ToInt32(chartNameToCursorValue[chartlegtxt].rightValue)].ToString();
                                }
                                else
                                {
                                    this.txt_left_value.Text = "";
                                    this.txt_right_value.Text = "";
                                }
                            }
                            else
                            {
                                this.dataGridView1.Rows[i].Cells[1].Value = chartNameToCursorValue[chartlegtxt].leftValue + "Hz";
                                this.dataGridView1.Rows[i].Cells[2].Value = chartNameToCursorValue[chartlegtxt].rightValue + "Hz";
                                if (isaddyb)
                                {
                                    this.txt_left_value.Text = chartNameToCursorValue[chartlegtxt].leftValue.ToString();
                                    this.txt_right_value.Text = chartNameToCursorValue[chartlegtxt].rightValue.ToString();
                                }
                                else
                                {
                                    this.txt_left_value.Text = "";
                                    this.txt_right_value.Text = "";
                                }
                            }

                            DataFormat dataformat = new DataFormat();

                            //dataformat.X = testNameToData[testname].x_value_all.ToArray();
                            dataformat.Y = chartNameToCursorValue[chartlegtxt].Y_values.ToArray();

                            dataformat.Unit = nowUnit;
                            double rms_value = Math.Round(rms.Dispathcer(dataformat), 3);
                            this.dataGridView1.Rows[i].Cells[3].Value = rms_value + nowUnit;
                            this.dataGridView1.Rows[i].Cells[4].Value = chartNameToCursorValue[chartlegtxt].Max_Value;
                        }
                    }
                }
            }
            catch (Exception)
            {
                if (this.chart1.Series.Count > 0)
                {
                    IsDisplayDoubleCursorValue(false);
                    if (isaddyb)
                    {

                        isaddyb = false;

                    }
                    IsDisplayDoubleCursorValue(false);
                    yb1.Points.Clear();
                    yb2.Points.Clear();
                    if (this.chart1.Series.Contains(yb1))
                    {

                        this.chart1.Series.Remove(yb1);
                    }
                    if (this.chart1.Series.Contains(yb2))
                    {
                        this.chart1.Series.Remove(yb2);
                    }
                    for (int i = 0; i < this.chart1.Series.Count; i++)
                    {
                        if (this.chart1.Series[i].Name.Contains("游标"))
                        {
                            this.chart1.Series[i].Points.Clear();
                            this.chart1.Series.Remove(this.chart1.Series[i]);
                            i--;
                        }
                    }

                    this.chart1.ChartAreas[0].CursorX.LineColor = Color.Black;
                    this.chart1.ChartAreas[0].CursorX.LineWidth = 1;
                    this.toolTip1.RemoveAll();
                }

            }
        }

        public void RemoveToolTip()
        {
            this.toolTip1.RemoveAll();
        }
        private void chart1_CursorPositionChanging(object sender, System.Windows.Forms.DataVisualization.Charting.CursorEventArgs e)
        {
            //DispalyCursorPosition();
        }

        private void frmLineChart_offline_Load(object sender, EventArgs e)
        {
            yb1.Name = "双游标1";
            yb1.ChartType = SeriesChartType.Line;
            yb1.Color = Color.Red;
            yb1.IsVisibleInLegend = false;
            yb2.Name = "双游标2";
            yb2.ChartType = SeriesChartType.Line;
            yb2.Color = Color.Red;
            yb2.IsVisibleInLegend = false;
            this.chart1.Series.Clear();
            this.chart1.ChartAreas[0].AxisX.Maximum = 5;
            this.chart1.ChartAreas[0].AxisX.Minimum = 1;
            this.chart1.ChartAreas[0].AxisY.Minimum = 1;
        }

        private void chart1_DragOver(object sender, DragEventArgs e)
        {
            if (!e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
            {

                e.Effect = DragDropEffects.None;
                return;
            }
            else
            {
                e.Effect = DragDropEffects.Move;  //这个值会返回给DoDragDrop方法
            }
        }


        public void DisplayChart(Cls_Data clsdata)
        {

            //this.chart1.Series[0].Points.Clear();
            List<float> displayvalue = new List<float>();

            if (Nowchannlname.Contains("频谱"))
            {
                nowChartName = "频谱";
                x_value = new double[clsdata.AccelerationFFTAmpDB.Count];
                y_value = new double[clsdata.AccelerationFFTAmpDB.Count];
                x_value_all = new double[clsdata.AccelerationFFTAmpDB.Count];
                y_value_all = new double[clsdata.AccelerationFFTAmpDB.Count];
                displayvalue = clsdata.AccelerationFFTAmpDB;
                if (this.chart1.ChartAreas[0].AxisX.Maximum < clsdata.AccelerationFFTAmpDB.Count)
                {
                    this.chart1.ChartAreas[0].AxisX.Maximum = clsdata.AccelerationFFTAmpDB.Count;
                }

                this.chart1.ChartAreas[0].AxisX.Minimum = 1;
                this.对数坐标ToolStripMenuItem.Visible = true;
                this.线性坐标ToolStripMenuItem.Visible = true;
                //addCustLabels_xx();
            }
            else if (Nowchannlname.Contains("时域"))
            {
                nowChartName = "时域";
                x_value = new double[clsdata.AccelerationTimewave.Count];
                y_value = new double[clsdata.AccelerationTimewave.Count];
                x_value_all = new double[clsdata.AccelerationTimewave.Count];
                y_value_all = new double[clsdata.AccelerationTimewave.Count];
                displayvalue = clsdata.AccelerationTimewave;
                if (this.chart1.ChartAreas[0].AxisX.Maximum < clsdata.AccelerationTimewave.Count)
                {
                    this.chart1.ChartAreas[0].AxisX.Maximum = clsdata.AccelerationTimewave.Count;
                }
                this.chart1.ChartAreas[0].AxisX.Minimum = 1;
                //addCustLabels_xx();
                this.对数坐标ToolStripMenuItem.Visible = false;
                this.线性坐标ToolStripMenuItem.Visible = false;
            }
            else if (Nowchannlname.Contains("倍频程"))
            {
                nowChartName = "1/3倍频程";
                x_value = new double[clsdata._1_3octavedB.Count];
                y_value = new double[clsdata._1_3octavedB.Count];
                x_value_all = new double[clsdata._1_3octavedB.Count];
                y_value_all = new double[clsdata._1_3octavedB.Count];
                displayvalue = clsdata._1_3octavedB;
                if (this.chart1.ChartAreas[0].AxisX.Maximum < clsdata._1_3octavedB.Count)
                {
                    this.chart1.ChartAreas[0].AxisX.Maximum = clsdata._1_3octavedB.Count;
                }
                this.chart1.ChartAreas[0].AxisX.Minimum = 0;
                //addCustLabels_xx();
                this.对数坐标ToolStripMenuItem.Visible = false;
                this.线性坐标ToolStripMenuItem.Visible = false;
            }
            else
            {
                nowChartName = "原始数据";
                string filePath = Mdl_Class.FilePath.Split('.')[0] + "\\" + nowsectionName + "\\" + nowtestName;
                string fileName = "原始数据_" + nowchannel + ".ini";
                displayvalue = opertext.ReadOldData(filePath + "\\" + fileName);
                x_value = new double[displayvalue.Count];
                y_value = new double[displayvalue.Count];
                x_value_all = new double[displayvalue.Count];
                y_value_all = new double[displayvalue.Count];
                this.对数坐标ToolStripMenuItem.Visible = false;
                this.线性坐标ToolStripMenuItem.Visible = false;
            }
            //addCustLabels_xx();
            if (displayvalue != null && displayvalue.Count > 0)
            {
                try
                {


                    for (int i = 0; i < displayvalue.Count; i++)
                    {
                        //x_value[i] = Math.Round((i + 1) / 5.12, 2);
                        //x_value_all[i] = Math.Round((i + 1) / 5.12, 2);

                        if (Nowchannlname.Contains("频谱"))
                        {
                            x_value[i] = Math.Round((double)(i + 1) * Convert.ToDouble(runset.Resolution), 3);
                            x_value_all[i] = Math.Round((double)(i + 1) * Convert.ToDouble(runset.Resolution), 3);
                            y_value[i] = Math.Round(Convert.ToDouble(displayvalue[i]), 2);
                            y_value_all[i] = Math.Round(Convert.ToDouble(displayvalue[i]), 2);
                        }
                        else if (Nowchannlname.Contains("倍频程"))
                        {
                            x_value[i] = i ;
                            x_value_all[i] = i;
                            y_value[i] = Math.Round(Convert.ToDouble(displayvalue[i]), 2);
                            y_value_all[i] = Math.Round(Convert.ToDouble(displayvalue[i]), 2);
                            if (i == 31)
                            {
                                break;
                            }
                        }
                        else
                        {
                            x_value[i] = (double)(i + 1) / (Convert.ToDouble(runset.BandWidth) * 2.0);
                            x_value_all[i] = (double)(i + 1) / (Convert.ToDouble(runset.BandWidth) * 2.0);
                            y_value[i] = Math.Round(Convert.ToDouble(displayvalue[i]), 4);
                            y_value_all[i] = Math.Round(Convert.ToDouble(displayvalue[i]), 4);
                        }
                        //y_value[i] = 0;
                        //y_value_all[i] = 0;
                        if (mindata == 0)
                        {
                            mindata = y_value[i];
                        }
                        else if (mindata > y_value[i])
                        {
                            mindata = y_value[i];
                        }
                        if (maxdata == 0)
                        {
                            maxdata = y_value[i];
                        }
                        else if (maxdata < y_value[i])
                        {
                            maxdata = y_value[i];
                        }


                    }

                    //if (this.chart1.ChartAreas[0].AxisY.Minimum >mindata)
                    //{
                    if (Nowchannlname.Contains("频谱"))
                    {
                        this.chart1.ChartAreas[0].AxisY.Minimum = mindata - 10;
                    }
                    else if (Nowchannlname.Contains("倍频程"))
                    { }
                    else
                    {
                        this.chart1.ChartAreas[0].AxisY.Minimum = mindata * 1.5;
                    }

                    //}
                    ////if (this.chart1.ChartAreas[0].AxisY.Maximum< maxdata)
                    ////{
                    if (Nowchannlname.Contains("频谱"))
                    {
                        this.chart1.ChartAreas[0].AxisY.Maximum = maxdata + 10;
                    }
                    else if (Nowchannlname.Contains("倍频程"))
                    {

                    }
                    else
                    {
                        this.chart1.ChartAreas[0].AxisY.Maximum = maxdata * 1.5;
                    }
                    //}
                    if (Nowchannlname.Contains("频谱"))
                    {
                        this.chart1.ChartAreas[0].AxisX.Maximum = Convert.ToDouble(runset.BandWidth);
                    }
                    else if (Nowchannlname.Contains("倍频程"))
                    {
                        this.chart1.ChartAreas[0].AxisX.Interval = 1;
                    }
                    else
                    {
                        this.chart1.ChartAreas[0].AxisX.Maximum = Convert.ToInt32(x_value.ToArray().Max());
                    }
                    if (Nowchannlname.Contains("原始数据") || Nowchannlname.Contains("时域"))
                    {
                        this.chart1.ChartAreas[0].AxisX.Title = "频率(s)";
                        this.chart1.ChartAreas[0].AxisY.Title = "";
                    }
                    else if (Nowchannlname.Contains("倍频程"))
                    {
                        this.chart1.ChartAreas[0].AxisX.Title = "Octave 1/3(Hz)";
                        this.chart1.ChartAreas[0].AxisY.Title = nowUnit;
                    }
                    else
                    {
                        //this.chart1.ChartAreas[0].AxisX.Title = "单位(HZ)";
                        this.chart1.ChartAreas[0].AxisY.Title = nowUnit;
                    }
                    testTodata testdata = new testTodata();
                    testdata.x_value = x_value.ToList();
                    testdata.y_value = y_value.ToList();
                    testdata.x_value_all = x_value_all.ToList();
                    testdata.y_value_all = y_value_all.ToList();
                    testNameToData.Add(testname, testdata);


                    DataBindChart(x_value, y_value);

                }
                catch (Exception ex)
                {

                    MessageBox.Show("数据存在错误,无法显示", "提示");
                }
            }
            if (maxdata == 0)
            {
                maxdata = 5;
            }

        }


        //返回颜色信息
        public Color ReturnColor()
        {


            if (!testNameToColor.ContainsValue(Color.DarkOrange))
            {
                return Color.DarkOrange;
            }

            if (!testNameToColor.ContainsValue(Color.Blue))
            {
                return Color.Blue;
            }

            if (!testNameToColor.ContainsValue(Color.Green))
            {
                return Color.Green;
            }


            if (!testNameToColor.ContainsValue(Color.DarkViolet))
            {
                return Color.DarkViolet;
            }


            if (!testNameToColor.ContainsValue(Color.DarkGoldenrod))
            {
                return Color.DarkGoldenrod;
            }


            if (!testNameToColor.ContainsValue(Color.Aqua))
            {
                return Color.Aqua;
            }


            if (!testNameToColor.ContainsValue(Color.Yellow))
            {
                return Color.Yellow;
            }


            if (!testNameToColor.ContainsValue(Color.Brown))
            {
                return Color.Brown;
            }


            if (!testNameToColor.ContainsValue(Color.Coral))
            {
                return Color.Coral;
            }


            if (!testNameToColor.ContainsValue(Color.DarkKhaki))
            {
                return Color.DarkKhaki;
            }


            if (!testNameToColor.ContainsValue(Color.Beige))
            {
                return Color.Beige;
            }
            else
            {
                return Color.Chartreuse;
            }


        }

        //绑定数据到图表上
        public void DataBindChart(double[] xvalue, double[] yvalue)
        {
            Series ser = new Series();
            ser.ChartType = SeriesChartType.Line;
            ser.Color = ReturnColor();
            ser.Points.DataBindXY(xvalue, yvalue);
            if (Nowchannlname.Contains("倍频程"))
            {
                for (int i = 0; i < ser.Points.Count; i++)
                {
                    ser.Points[i].AxisLabel = axis_txt[i].ToString();
                }
            }
            //this.chart1.Series[0].Points.DataBindXY(xvalue, yvalue);
            ser.Name = testname;
            ser.LegendText = testname;
            testNameToSeries.Add(testname, ser);
            testNameToColor.Add(testname, ser.Color);

            if (Now_Type != "")
            {
                if (nowUnit == "dB(m/s2)" || nowUnit == "dB(mm/s)" || nowUnit == "m/s2" || nowUnit == "mm/s")
                {

                    switch (Now_Type)
                    {
                        case "单位转换":
                            DataFormat dataformat1 = new DataFormat();
                            dataformat1.Unit = nowUnit;
                            dataformat1.X = testNameToData[testname].x_value_all.ToArray();
                            dataformat1.Y = testNameToData[testname].y_value_all.ToArray();
                            DataFormat dt_new = unitconverter.UnitDispatcher(dataformat1);
                            testNameToSeries[testname].Points.Clear();
                            testNameToSeries[testname].Points.DataBindXY(testNameToData[testname].x_value_all, dt_new.Y);

                            //testNameToData[item].x_value = dt_new.X.ToList();
                            //testNameToData[item].x_value_all = dt_new.X.ToList();
                            testNameToData[testname].y_value = dt_new.Y.ToList();
                            testNameToData[testname].y_value_all = dt_new.Y.ToList();

                            //this.chart1.ChartAreas[0].AxisX.Title = "单位(HZ)";
                            this.chart1.ChartAreas[0].AxisY.Title = dt_new.Unit;
                            //this.chart1.ChartAreas[0].AxisX.Maximum = dt_new.Y.ToArray().Max() * 1.5;
                            nowUnit = dt_new.Unit;
                            this.chart1.Series.Add(testNameToSeries[testname]);
                            this.chart1.ChartAreas[0].AxisY.Maximum = dt_new.Y.ToArray().Max() * 1.5;
                            this.chart1.ChartAreas[0].AxisY.Minimum = dt_new.Y.ToArray().Min() / 1.5;

                            break;
                        case "积分":


                            DataFormat dataformat2 = new DataFormat();
                            dataformat2.Unit = nowUnit;
                            dataformat2.X = testNameToData[testname].x_value_all.ToArray();
                            dataformat2.Y = testNameToData[testname].y_value_all.ToArray();
                            DataFormat dt_new2 = unitconverter.IntegrationDispatcher(dataformat2);
                            testNameToSeries[testname].Points.Clear();
                            testNameToSeries[testname].Points.DataBindXY(testNameToData[testname].x_value_all, dt_new2.Y);
                            //testNameToData[item].x_value = dt_new2.X.ToList();
                            //testNameToData[item].x_value_all = dt_new2.X.ToList();
                            testNameToData[testname].y_value = dt_new2.Y.ToList();
                            testNameToData[testname].y_value_all = dt_new2.Y.ToList();

                            //this.chart1.ChartAreas[0].AxisX.Title = "单位(HZ)";
                            this.chart1.ChartAreas[0].AxisY.Title = dt_new2.Unit;
                            nowUnit = dt_new2.Unit;
                            this.chart1.Series.Add(testNameToSeries[testname]);

                            this.chart1.ChartAreas[0].AxisY.Maximum = dt_new2.Y.ToArray().Max() * 1.5;
                            this.chart1.ChartAreas[0].AxisY.Minimum = dt_new2.Y.ToArray().Min() / 1.5;
                            break;
                        default:
                            break;
                    }

                }
            }
            else
            {
                this.chart1.Series.Add(ser);
            }
            DataFormat dataformat = new DataFormat();
            dataformat.X = testNameToData[testname].x_value_all.ToArray();
            dataformat.Y = testNameToData[testname].y_value_all.ToArray();
            dataformat.Unit = nowUnit;
            double rms_value = Math.Round(rms.Dispathcer(dataformat), 3);

            double maxvalue = testNameToData[testname].y_value_all.Max();
            int maxindex = testNameToData[testname].y_value_all.ToList().IndexOf(maxvalue);
            //string Max_Values = maxvalue.ToString() + nowUnit+" @ "+ testNameToData[testname].x_value_all[maxindex].ToString() + "Hz";
            string Max_Values = "";
            if (Nowchannlname.Contains("倍频程"))
            {
                Max_Values = maxvalue.ToString() + nowUnit + " @ " + axis_txt[Convert.ToInt32(testNameToData[testname].x_value_all[maxindex])].ToString() + "Hz";
                this.dataGridView1.Rows.Add(testname, axis_txt[Convert.ToInt32(testNameToData[testname].x_value_all.Min())].ToString() + "Hz", axis_txt[Convert.ToInt32(testNameToData[testname].x_value_all.Max())].ToString() + "Hz", rms_value + nowUnit, Max_Values, "", "删除");
            }
            else
            {
                Max_Values = maxvalue.ToString() + nowUnit + " @ " + testNameToData[testname].x_value_all[maxindex].ToString() + "Hz";
                this.dataGridView1.Rows.Add(testname, testNameToData[testname].x_value_all.Min() + "Hz", testNameToData[testname].x_value_all.Max() + "Hz", rms_value + nowUnit, Max_Values, "", "删除");
            }
            this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Cells[5].Style.BackColor = testNameToSeries[testname].Color;

            ismove = true;
            DispalyCursorPosition(true);


        }


        public void ReflashRMS()
        {
            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {


                DataFormat dataformat = new DataFormat();
                dataformat.X = testNameToData[testname].x_value_all.ToArray();
                dataformat.Y = testNameToData[testname].y_value_all.ToArray();
                dataformat.Unit = nowUnit;
                double rms_value = Math.Round(rms.Dispathcer(dataformat), 3);
                double maxvalue = testNameToData[testname].y_value_all.Max();
                int maxindex = testNameToData[testname].y_value_all.ToList().IndexOf(maxvalue);
                //string Max_Values = maxvalue.ToString() + nowUnit+" @ "+ testNameToData[testname].x_value_all[maxindex].ToString() + "Hz";
                string Max_Values = "";
                if (Nowchannlname.Contains("倍频程"))
                {
                    Max_Values = Math.Round(maxvalue,5).ToString() + nowUnit + " @ " + Math.Round(axis_txt[Convert.ToInt32(testNameToData[testname].x_value_all[maxindex])],5).ToString() + "Hz";
                }
                else
                {
                    Max_Values = Math.Round(maxvalue,5).ToString() + nowUnit + " @ " + Math.Round(testNameToData[testname].x_value_all[maxindex],5).ToString() + "Hz";

                }
                this.dataGridView1.Rows[i].Cells[1].Value = testNameToData[testname].x_value_all.Min() + "Hz";
                this.dataGridView1.Rows[i].Cells[2].Value = testNameToData[testname].x_value_all.Max() + "Hz";
                this.dataGridView1.Rows[i].Cells[3].Value = rms_value + nowUnit;
                this.dataGridView1.Rows[i].Cells[4].Value = Max_Values;

            }
        }




        //动态增加刻度(对数)
        public void addCustLabels_ds()
        {
            string[] custText = new string[] { "10", "20", "30", "40", "50", "60", "70", "80", "90", "100", "200", "300", "400", "500", "600", "700", "800", "900", "1K", "2K", "3K", "4K", "5K", "6K", "7K", "8K", "9K", "10K" };
            double[] custPositions = new double[] {2,2.602059991,2.954242509,3.204119983,3.397940009,3.556302501,3.69019608,3.806179974
,3.908485019
,4
,4.602059991
,4.954242509
,5.204119983
,5.397940009
,5.556302501
,5.69019608
,5.806179974
,5.908485019
,6
,6.602059991
,6.954242509
,7.204119983
,7.397940009
,7.556302501
,7.69019608
,7.806179974
,7.908485019
,8
            };

            for (int i = 0; i < custText.Length; i++)
            {
                CustomLabel custlabel = new CustomLabel();
                custlabel.ForeColor = Color.Black;
                custlabel.FromPosition = custPositions[i];
                custlabel.GridTicks = GridTickTypes.All;
                custlabel.Text = custText[i];
                this.chart1.ChartAreas[0].AxisX.CustomLabels.Add(custlabel);
            }

        }


        public void addCustLabels_xx()
        {

            string[] custText = null;
            double[] custPositions = null;
            int maxvalue = Convert.ToInt32(this.chart1.ChartAreas[0].AxisX.Maximum);
            switch (maxvalue)
            {
                case 256:
                    custText = new string[] { "1", "12.5", "25", "50" };

                    custPositions = new double[] { 5, 64, 128, 256 };
                    break;
                case 512:
                    custText = new string[] { "1", "25", "50", "100" };
                    custPositions = new double[] { 5, 128, 256, 512 };
                    break;
                case 1024:
                    custText = new string[] { "1", "50", "100", "150", "200" };
                    custPositions = new double[] { 5, 256, 512, 768, 1024 };
                    break;
                case 2048:
                    custText = new string[] { "1", "100", "200", "300", "400" };
                    custPositions = new double[] { 5, 512, 1024, 1536, 2048 };
                    break;
                case 4096:
                    custText = new string[] { "1", "100", "200", "300", "400", "500", "600", "700", "800" };
                    custPositions = new double[] { 5, 512, 1024, 1536, 2048, 2560, 3072, 3584, 4096 };
                    break;
                case 8192:
                    custText = new string[] { "1", "200", "400", "600", "800", "1K", "1.2K", "1.4K", "1.6K" };
                    custPositions = new double[] { 5, 1024, 2048, 3072, 4096, 5120, 6144, 7168, 8192 };
                    break;

                case 16384:
                    custText = new string[] { "1", "400", "800", "1.2K", "1.6K", "2K", "2.4K", "2.8K", "3.2K" };
                    custPositions = new double[] { 5, 2048, 4096, 6144, 8192, 10240, 12288, 14336, 16384 };
                    break;
                case 32768:
                    custText = new string[] { "1", "800", "1.6K", "2.4K", "3.2K", "4K", "4.8K", "5.6K", "6.4K" };
                    custPositions = new double[] { 5, 4096, 8192, 12288, 16384, 20480, 24576, 28672, 32768 };
                    break;
                case 65536:
                    custText = new string[] { "1", "2K", "4K", "6K", "7K", "8K", "9K", "10K", "11K", "12K" };
                    custPositions = new double[] { 5, 10240, 20480, 30720, 35840, 40960, 46080, 51200, 56320, 61440 };
                    break;
                case 131072:
                    custText = new string[] { "1", "2K", "4K", "6K", "8K", "10K", "12K", "14K", "16K", "18K", "20K", "22K", "24K" };
                    custPositions = new double[] { 5, 10240, 20480, 30720, 40960, 51200, 61440, 71680, 81920, 92160, 102400, 112640, 122880 };
                    break;
                default:
                    break;
            }
            for (int i = 0; i < custText.Length; i++)
            {
                CustomLabel custlabel = new CustomLabel();
                custlabel.ForeColor = Color.Black;
                custlabel.FromPosition = custPositions[i] * 2;
                custlabel.GridTicks = GridTickTypes.All;
                custlabel.Text = custText[i];
                this.chart1.ChartAreas[0].AxisX.CustomLabels.Add(custlabel);
            }


        }

        private void chart1_MouseLeave(object sender, EventArgs e)
        {
            this.toolTip1.RemoveAll();
        }

        private void chart1_Leave(object sender, EventArgs e)
        {
            this.toolTip1.RemoveAll();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.chart1.Series.Count > 0) {
                IsDisplayDoubleCursorValue(false);
                if (isaddyb)
                {

                    isaddyb = false;

                }
                IsDisplayDoubleCursorValue(false);
                IsDiplayDYB(true);
                yb1.Points.Clear();
                yb2.Points.Clear();
                if (this.chart1.Series.Contains(yb1))
                {

                    this.chart1.Series.Remove(yb1);
                }
                if (this.chart1.Series.Contains(yb2))
                {
                    this.chart1.Series.Remove(yb2);
                }
                for (int i = 0; i < this.chart1.Series.Count; i++)
                {
                    if (this.chart1.Series[i].Name.Contains("游标"))
                    {
                        this.chart1.Series[i].Points.Clear();
                        this.chart1.Series.Remove(this.chart1.Series[i]);
                        i--;
                    }
                }

                this.chart1.ChartAreas[0].CursorX.LineColor = Color.Black;
                this.chart1.ChartAreas[0].CursorX.LineWidth = 1;
                this.toolTip1.RemoveAll();
            }
        }

        private void 添加双游标ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.chart1.Series.Count > 0)
            {
                if (isaddyb == false)
                {
                    isaddyb = true;
                    IsDisplayDoubleCursorValue(true);
                    IsDiplayDYB(false);
                    double yb_x_location = (double)Convert.ToInt32(this.chart1.ChartAreas[0].AxisX.Maximum / 2);

                    //double[] yx_x = new double[] { yb_x_location, yb_x_location };
                    //double[] yx_y = new double[] { this.chart1.ChartAreas[0].AxisY.Minimum, this.chart1.ChartAreas[0].AxisY.Maximum };


                    yb1.Points.AddXY(yb_x_location, this.chart1.ChartAreas[0].AxisY.Minimum + (this.chart1.ChartAreas[0].AxisY.Minimum) * 0.00001);
                    yb1.Points.AddXY(yb_x_location, this.chart1.ChartAreas[0].AxisY.Maximum - (this.chart1.ChartAreas[0].AxisY.Maximum) * 0.00001);

                    if (yb_x_location + (this.chart1.ChartAreas[0].AxisX.Maximum / 10) < this.chart1.ChartAreas[0].AxisX.Maximum)
                    {
                        yb2.Points.AddXY(yb_x_location + (this.chart1.ChartAreas[0].AxisX.Maximum / 10), this.chart1.ChartAreas[0].AxisY.Minimum + (this.chart1.ChartAreas[0].AxisY.Minimum) * 0.00001);
                        yb2.Points.AddXY(yb_x_location + (this.chart1.ChartAreas[0].AxisX.Maximum / 10), this.chart1.ChartAreas[0].AxisY.Maximum - (this.chart1.ChartAreas[0].AxisY.Maximum) * 0.00001);
                    }
                    else
                    {
                        yb2.Points.AddXY(yb_x_location - (this.chart1.ChartAreas[0].AxisX.Maximum / 10), this.chart1.ChartAreas[0].AxisY.Minimum + (this.chart1.ChartAreas[0].AxisY.Minimum) * 0.00001);
                        yb2.Points.AddXY(yb_x_location - (this.chart1.ChartAreas[0].AxisX.Maximum / 10), this.chart1.ChartAreas[0].AxisY.Maximum - (this.chart1.ChartAreas[0].AxisY.Maximum) * 0.00001);
                    }
                    this.chart1.ChartAreas[0].CursorX.LineColor = Color.White;
                    this.chart1.ChartAreas[0].CursorX.LineWidth = 0;
                    this.chart1.Series.Add(yb1);
                    this.chart1.Series.Add(yb2);
                    Point _mousePoint = Control.MousePosition;
                    int _x = this.chart1.PointToClient(_mousePoint).X;
                    int _y = this.chart1.PointToClient(_mousePoint).Y;

                    string toolText = "";
          
                        this.toolTip1.Show(toolText, this.chart1, _x, _y);
                    
                    Dictionary<string, CursorValue> chartNameToCursorValue = new Dictionary<string, CursorValue>();
                    for (int i = 0; i < this.chart1.Series.Count; i++)
                    {
                        if (!this.chart1.Series[i].LegendText.Contains("游标") && !this.chart1.Series[i].Name.Contains("游标"))
                        {
                            int y1_value = 0;
                            int y2_value = 0;
                            if (Nowchannlname.Contains("倍频程"))
                            {
                                y1_value = Convert.ToInt32(yb1.Points[0].XValue);
                                y2_value = Convert.ToInt32(yb2.Points[0].XValue);
                            }
                            else
                            {
                                if (chart_Type == 1)
                                {
                                    //y1_value = Convert.ToInt32((Convert.ToInt32(Math.Pow(10, yb1.Points[0].XValue) / Convert.ToDouble(runset.Resolution))));
                                    //y2_value= Convert.ToInt32((Convert.ToInt32(Math.Pow(10, yb2.Points[0].XValue) / Convert.ToDouble(runset.Resolution))));

                                    y1_value = Convert.ToInt32((Convert.ToInt32(yb1.Points[0].XValue / Convert.ToDouble(runset.Resolution))));
                                    y2_value = Convert.ToInt32((Convert.ToInt32(yb2.Points[0].XValue / Convert.ToDouble(runset.Resolution))));
                                }
                                else
                                {
                                    y1_value = Convert.ToInt32((Convert.ToInt32(yb1.Points[0].XValue / Convert.ToDouble(runset.Resolution))));
                                    y2_value = Convert.ToInt32((Convert.ToInt32(yb2.Points[0].XValue / Convert.ToDouble(runset.Resolution))));
                                }
                            }
                            CursorValue cv = GetCursorValue(this.chart1.Series[i], y1_value, y2_value, Math.Round(yb1.Points[0].XValue, 2), Math.Round(yb2.Points[0].XValue, 2));
                            toolText = toolText + this.chart1.Series[i].LegendText + " X左:" + Math.Round(yb1.Points[0].XValue, 2).ToString() + " X右:" + Math.Round(yb2.Points[0].XValue, 2).ToString()
                                + " 最大值:" + cv.Max_Value.ToString() + " 最小值:" + cv.Min_Value.ToString() + " 平均值:" + cv.Avg_Value.ToString() + "\n";

                            chartNameToCursorValue.Add(this.chart1.Series[i].LegendText, cv);
                        }
                    }
                    toolText = "";
                    //this.toolTip1.Show(toolText, this.chart1, _x, _y);

                    for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                    {
                        string chartlegtxt = this.dataGridView1.Rows[i].Cells[0].Value.ToString();
                        if (chartNameToCursorValue.ContainsKey(chartlegtxt))
                        {
                            if (Nowchannlname.Contains("倍频程"))
                            {
                                this.dataGridView1.Rows[i].Cells[1].Value = axis_txt[Convert.ToInt32(chartNameToCursorValue[chartlegtxt].leftValue)] + "Hz";
                                this.dataGridView1.Rows[i].Cells[2].Value = axis_txt[Convert.ToInt32(chartNameToCursorValue[chartlegtxt].rightValue)] + "Hz";
                                if (isaddyb)
                                {
                                    this.txt_left_value.Text = axis_txt[Convert.ToInt32(chartNameToCursorValue[chartlegtxt].leftValue)].ToString();
                                    this.txt_right_value.Text = axis_txt[Convert.ToInt32(chartNameToCursorValue[chartlegtxt].rightValue)].ToString();
                                }
                                else
                                {
                                    this.txt_left_value.Text = "";
                                    this.txt_right_value.Text = "";
                                }
                            }
                            else
                            {
                                this.dataGridView1.Rows[i].Cells[1].Value = chartNameToCursorValue[chartlegtxt].leftValue + "Hz";
                                this.dataGridView1.Rows[i].Cells[2].Value = chartNameToCursorValue[chartlegtxt].rightValue + "Hz";
                                if (isaddyb)
                                {
                                    this.txt_left_value.Text = chartNameToCursorValue[chartlegtxt].leftValue.ToString();
                                    this.txt_right_value.Text = chartNameToCursorValue[chartlegtxt].rightValue.ToString();
                                }
                                else
                                {
                                    this.txt_left_value.Text = "";
                                    this.txt_right_value.Text = "";
                                }
                            }


                            DataFormat dataformat = new DataFormat();
                            //dataformat.X = testNameToData[testname].x_value_all.ToArray();
                            dataformat.Y = chartNameToCursorValue[chartlegtxt].Y_values.ToArray();
                            dataformat.Unit = nowUnit;
                            double rms_value = Math.Round(rms.Dispathcer(dataformat), 3);
                            this.dataGridView1.Rows[i].Cells[3].Value = rms_value + nowUnit;



                            this.dataGridView1.Rows[i].Cells[4].Value = chartNameToCursorValue[chartlegtxt].Max_Value;
                        }
                    }
                }
            }
        }


        public void IsDisplayDoubleCursorValue(bool isdisplay)
        {
            if (isdisplay)
            {
                this.label1.Visible = true;
                this.textBox1.Visible = true;
                this.txt_left_value.Visible = true;
          
                this.label3.Visible = true;
                this.txt_right_value.Visible = true;
   
                this.button1.Visible = true;
            }
            else
            {
                this.label1.Visible = false;
                //this.textBox1.Visible = false;
                this.txt_left_value.Visible = false;
       
                this.label3.Visible = false;
                this.txt_right_value.Visible = false;

                this.button1.Visible = false;
            }
        }

        //public string GetHz(double pointvalue) 
        //{
        //    double hz_value=Math.Round(pointvalue/5.12,1);
        //    if (hz_value >= 1000)
        //    {

        //        return (Math.Round(hz_value / 1000.0, 1)).ToString() + "K";
        //    }
        //    else 
        //    {
        //        return hz_value.ToString();
        //    }
        //}

        bool ismove = false;
        Series movSeries = null;
        bool CursorPositionIsTrue = false;
        private void chart1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                CursorPositionIsTrue = true;
                if (isaddyb == true)
                {
                    ismove = true;
                    double ybz = 0;
                    if (chart_Type == 1) {
                        ybz = Math.Pow(10, this.chart1.ChartAreas[0].CursorX.Position);
                    }
                    else
                    {
                        ybz = this.chart1.ChartAreas[0].CursorX.Position;
                    }
                    if (Math.Abs(ybz - yb1.Points[0].XValue) < Math.Abs(ybz - yb2.Points[0].XValue))
                    {
                        movSeries = yb1;
                    }
                    else
                    {
                        movSeries = yb2;
                    }
                }
            } else if (e.Button == MouseButtons.Right)
            {

                Console.WriteLine("X：" + e.X.ToString() + "  Y:" + e.Y + " 光标X：" + e.Location.X.ToString() + " Y:" + e.Location.Y.ToString());
                if (e.X < 110)
                {

                    this.chart1.ContextMenuStrip = this.contextMenuStrip1;
                    this.contextMenuStrip1.Items[4].Visible = true;
                    this.contextMenuStrip1.Items[5].Visible = true;
                    this.contextMenuStrip1.Items[6].Visible = false;
                    this.contextMenuStrip1.Items[7].Visible = false;
                    this.contextMenuStrip1.Items[0].Visible = false;
                    this.contextMenuStrip1.Items[1].Visible = false;
                    this.contextMenuStrip1.Items[2].Visible = false;
                    this.contextMenuStrip1.Items[3].Visible = false;
                    this.contextMenuStrip1.Items[8].Visible = false;
                    this.contextMenuStrip1.Items[9].Visible = false;

                }
                else
                {
                    this.contextMenuStrip1.Items[4].Visible = false;
                    this.contextMenuStrip1.Items[5].Visible = false;
                    this.contextMenuStrip1.Items[6].Visible = true;
                    this.contextMenuStrip1.Items[7].Visible = true;
                    this.contextMenuStrip1.Items[8].Visible = false;
                    this.contextMenuStrip1.Items[9].Visible = false;


                    if (!chart_type.Contains("倍频程"))
                    {
                        this.contextMenuStrip1.Items[0].Visible = true;
                        this.contextMenuStrip1.Items[1].Visible = true;
                        this.contextMenuStrip1.Items[2].Visible = true;
                        this.contextMenuStrip1.Items[3].Visible = true;
                    }
                    else
                    {
                        this.contextMenuStrip1.Items[0].Visible = true;
                        this.contextMenuStrip1.Items[1].Visible = true;
                        this.contextMenuStrip1.Items[8].Visible = true;
                        this.contextMenuStrip1.Items[9].Visible = true;

                    }
                }
            }
        }


        private void chart1_MouseUp(object sender, MouseEventArgs e)
        {
            CursorPositionIsTrue = false;
            ismove = false;
        }

        private void Cha(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 获取游标区间值
        /// </summary>
        /// <returns></returns>
        public CursorValue GetCursorValue(Series ser, int min_location, int max_location, double leftvalue, double rightvalue)
        {
            try
            {


                double[] Y_Points_Value = testNameToData[ser.Name].y_value_all.ToArray().Skip(min_location).Take(max_location - min_location).ToArray();
                double[] X_Points_Value = testNameToData[ser.Name].x_value_all.ToArray().Skip(min_location).Take(max_location - min_location).ToArray();
                CursorValue curvalue = new CursorValue();
                curvalue.Y_values = Y_Points_Value;
                double maxvalue = Y_Points_Value.Max();
                int maxindex = Y_Points_Value.ToList().IndexOf(maxvalue);
                if (Nowchannlname.Contains("倍频程"))
                {
                    curvalue.Max_Value = Math.Round(maxvalue,5).ToString() + nowUnit + " @ " + Math.Round(axis_txt[Convert.ToInt32(X_Points_Value[maxindex])],5).ToString() + "Hz";
                }
                else
                {
                    curvalue.Max_Value = Math.Round(maxvalue,5).ToString() + nowUnit + " @ " + Math.Round(X_Points_Value[maxindex],5).ToString() + "Hz";
                }
                curvalue.Min_Value = Y_Points_Value.Min();
                curvalue.Avg_Value = Math.Round(Y_Points_Value.Average(), 3);
                curvalue.leftValue = leftvalue;
                curvalue.rightValue = rightvalue;
                return curvalue;
            }
            catch (Exception)
            {
                CursorValue curvalue = new CursorValue();
                curvalue.Max_Value = "";
                curvalue.Min_Value = 0;
                curvalue.Avg_Value = 0;

                return curvalue;
            }
        }

        private void chart1_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {

            if (CursorPositionIsTrue && isaddyb == false)
            {

                if (e.X<0)
                {
                    return;
                }

                double XVuale = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
                double YVuale = chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Y);
                //this.chart1.ChartAreas[0].CursorX.Position = move_x;
                DispalyCursorPosition(true);

            }
            else if (isaddyb)
            {
                if (movSeries != null && movSeries.Name == "双游标1")
                {

                    double cz = 0;

                    //yb1.Points[0].XValue = this.chart1.ChartAreas[0].CursorX.Position;
                    //yb1.Points[1].XValue = this.chart1.ChartAreas[0].CursorX.Position;

                    //yb2.Points[0].XValue = this.chart1.ChartAreas[0].CursorX.Position + cz;
                    //yb2.Points[1].XValue = this.chart1.ChartAreas[0].CursorX.Position + cz;
                    //}
                    double Curx_value = 0;

                    if (chart_Type == 1)
                    {
                        if (Math.Pow(10, this.chart1.ChartAreas[0].CursorX.Position + cz) < this.chart1.ChartAreas[0].AxisX.Maximum)
                        {



                            cz = Math.Log10(yb2.Points[0].XValue) - Math.Log10(yb1.Points[0].XValue);

                            Curx_value = Math.Pow(10, this.chart1.ChartAreas[0].CursorX.Position);

                            yb1.Points[0].XValue = Curx_value;
                            yb1.Points[1].XValue = Curx_value;
                            yb2.Points[0].XValue = Math.Pow(10, this.chart1.ChartAreas[0].CursorX.Position + cz);
                            yb2.Points[1].XValue = Math.Pow(10, this.chart1.ChartAreas[0].CursorX.Position + cz);
                        }
                    }
                    else
                    {
                        if (yb2.Points[0].XValue + cz < this.chart1.ChartAreas[0].AxisX.Maximum)
                        {
                            cz = yb2.Points[0].XValue - yb1.Points[0].XValue;
                            Curx_value = this.chart1.ChartAreas[0].CursorX.Position;

                            yb1.Points[0].XValue = Curx_value;
                            yb1.Points[1].XValue = Curx_value;

                            yb2.Points[0].XValue = Curx_value + cz;
                            yb2.Points[1].XValue = Curx_value + cz;
                        }
                    }

                }
                else if (movSeries != null && movSeries.Name == "双游标2")
                {
                    if (chart_Type == 1)
                    {
                        double Curx_value = Math.Pow(10, this.chart1.ChartAreas[0].CursorX.Position);
                        yb2.Points[0].XValue = Curx_value;
                        yb2.Points[1].XValue = Curx_value;
                    }
                    else
                    {
                        double Curx_value = this.chart1.ChartAreas[0].CursorX.Position;

                        yb2.Points[0].XValue = Curx_value;
                        yb2.Points[1].XValue = Curx_value;
                    }
                }
            }

            }
            catch (Exception)
            {


            }
        }


 

        private void chart1_CursorPositionChanged(object sender, CursorEventArgs e)
        {
            if (isaddyb == true)
            {
            DispalyCursorPosition(true);
            }
        }

        private void 对数坐标ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.chart1.Series.Count > 0)
            {
                if (isaddyb)
                {

                    isaddyb = false;

                }
                IsDisplayDoubleCursorValue(false);
                IsDiplayDYB(true);
                for (int i = 0; i < this.chart1.Series.Count; i++)
                {
                    if (this.chart1.Series[i].Name.Contains("游标"))
                    {
                        this.chart1.Series[i].Points.Clear();
                        this.chart1.Series.RemoveAt(i);
                        i--;
                    }
                }
                this.chart1.ChartAreas[0].CursorX.LineColor = Color.Black;
                    this.chart1.ChartAreas[0].CursorX.LineWidth = 1;
                    this.toolTip1.RemoveAll();
                
                chart_Type = 1;
                this.chart1.ChartAreas[0].AxisX.IsLogarithmic = true;
                this.chart1.ChartAreas[0].CursorX.Interval = 0.001;
                ReflashRMS();
            }
        }

        private void 线性坐标ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.chart1.Series.Count > 0)
            {
              
                    if (isaddyb)
                    {

                        isaddyb = false;

                    }
                IsDiplayDYB(true);
                IsDisplayDoubleCursorValue(false);
                for (int i = 0; i < this.chart1.Series.Count; i++)
                {
                    if (this.chart1.Series[i].Name.Contains("游标"))
                    {
                        this.chart1.Series[i].Points.Clear();
                        this.chart1.Series.RemoveAt(i);
                        i--;
                    }
                }
                    this.chart1.ChartAreas[0].CursorX.LineColor = Color.Black;
                    this.chart1.ChartAreas[0].CursorX.LineWidth = 1;
                    this.toolTip1.RemoveAll();
                
                this.chart1.ChartAreas[0].AxisX.IsLogarithmic = false;
                this.chart1.ChartAreas[0].CursorX.Interval = 0.1;
                this.chart1.ChartAreas[0].AxisX.Maximum = Convert.ToDouble(runset.BandWidth);
                chart_Type = 0;


                ReflashRMS();
            }
        }

        private void bt_display_Click(object sender, EventArgs e)
        {

            if (YorN())
            {
             
            }
        }

        public void IsDiplayDYB(bool yorn)
        {
            if (yorn)
            {
                this.lb_dyb.Visible = true;
                this.txt_dyb.Visible = true;
                this.bt_dyb.Visible = true;
                this.lb_DYB_X_Y.Visible = true;

            }
            else
            {
                this.lb_dyb.Visible = false;
                this.txt_dyb.Visible = false;
                this.bt_dyb.Visible = false;
                this.lb_DYB_X_Y.Visible = false;
                this.lb_DYB_X_Y.Text = "";
                this.txt_dyb.Text = "";
            }

        }

        public bool YorN()
        {
            bool yorn = true;
            if (this.textBox1.Text.Trim() == "" && this.textBox2.Text.Trim() == "")
            {
                MessageBox.Show("范围值不能为空", "提示");
                yorn = false;
            }
            else
            {
                try
                {
                    if (this.chart1.Series.Count > 0)
                    {
                        if (isaddyb)
                        {
                            this.chart1.Series.Remove(yb1);
                            this.chart1.Series.Remove(yb2);
                            yb1.Points.Clear();
                            yb2.Points.Clear();
                            isaddyb = false;
                            IsDisplayDoubleCursorValue(false);
                        }
                        this.chart1.ChartAreas[0].CursorX.LineColor = Color.Black;
                        this.chart1.ChartAreas[0].CursorX.LineWidth = 1;
                        this.toolTip1.RemoveAll();
                    }
                    double minvalue = Convert.ToDouble(this.textBox1.Text.Trim());
                    double maxvalue = Convert.ToDouble(this.textBox2.Text.Trim());
                    if (minvalue < maxvalue)
                    {
                        max_data = maxvalue;
                        min_data = minvalue;
                    }
                    else
                    {
                        max_data = minvalue;
                        min_data = maxvalue;
                    }
                    if (min_data<=0)
                    {
                        MessageBox.Show("最小值必须大于0", "提示");
                        return false;
                    }
                    //Dispaly_SelectChart(minvalue, maxvalue);
                    if (min_data<0|| max_data> x_value.ToArray().Max()+1) 
                    {
                        MessageBox.Show("范围超出","提示");
                        return false;
                    }
                    this.chart1.ChartAreas[0].AxisX.Minimum = min_data;
                    this.chart1.ChartAreas[0].AxisX.Maximum = max_data;
                }
                catch (Exception ex)
                {

                    yorn = false;
                    MessageBox.Show("输入有误,只能输入小数或者整数", "提示");
                }
            }
            
            return yorn;
        }

        public bool YorN1()
        {
            bool yorn = true;
            if (this.textBox3.Text.Trim() == "" && this.textBox4.Text.Trim() == "")
            {
                MessageBox.Show("范围值不能为空", "提示");
                yorn = false;
            }
            else
            {
                try
                {
        
                    double minvalue = Convert.ToDouble(this.textBox3.Text.Trim());
                    double maxvalue = Convert.ToDouble(this.textBox4.Text.Trim());
                    if (minvalue < maxvalue)
                    {
                        max_data = maxvalue;
                        min_data = minvalue;
                    }
                    else
                    {
                        max_data = minvalue;
                        min_data = maxvalue;
                    }
                    //Dispaly_SelectChart(minvalue, maxvalue);
                    //if (min_data < y_value.ToArray().Min()|| max_data > y_value.ToArray().Max())
                    //{
                    //    MessageBox.Show("范围超出", "提示");
                    //    return false;
                    //}
                    this.chart1.ChartAreas[0].AxisY.Minimum = min_data;
                    this.chart1.ChartAreas[0].AxisY.Maximum = max_data;
                }
                catch (Exception ex)
                {

                    yorn = false;
                    MessageBox.Show("输入有误,只能输入小数或者整数", "提示");
                }
            }

            return yorn;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex>=0&&e.ColumnIndex==6)
            {
                if (e.RowIndex==0 && this.chart1.ChartAreas[0].AxisX.IsLogarithmic)
                {
                    if (this.chart1.Series.Count > 0)
                    {

                        if (isaddyb)
                        {

                            isaddyb = false;

                        }
                        IsDisplayDoubleCursorValue(false);
                        for (int i = 0; i < this.chart1.Series.Count; i++)
                        {
                            if (this.chart1.Series[i].Name.Contains("游标"))
                            {
                                this.chart1.Series[i].Points.Clear();
                                this.chart1.Series.RemoveAt(i);
                                i--;
                            }
                        }
                        this.chart1.ChartAreas[0].CursorX.LineColor = Color.Black;
                        this.chart1.ChartAreas[0].CursorX.LineWidth = 1;
                        this.toolTip1.RemoveAll();

                        this.chart1.ChartAreas[0].AxisX.IsLogarithmic = false;
                        this.chart1.ChartAreas[0].CursorX.Interval = 0.1;
                        this.chart1.ChartAreas[0].AxisX.Maximum = Convert.ToDouble(runset.BandWidth);
                        chart_Type = 0;


                        ReflashRMS();
                    }
            
                }
                string test_nn = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                if (testNameToData.ContainsKey(test_nn)) 
                {
                    testNameToData.Remove(test_nn);
                }
                if (testNameToSeries.ContainsKey(test_nn))
                {
                    this.chart1.Series.Remove(testNameToSeries[test_nn]);
                    testNameToSeries.Remove(test_nn);

                }
                if (testNameToColor.ContainsKey(test_nn))
                {
                    testNameToColor.Remove(test_nn);
                }

                this.dataGridView1.Rows.RemoveAt(e.RowIndex);

            }
        }

        private void chart1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && e.Clicks == 1)
            {
                this.label1.Text = "您单击了鼠标右键！";

                this.chart1.ContextMenuStrip = this.contextMenuStrip1;
                Console.WriteLine("X："+e.X.ToString()+"  Y:"+e.Y);
                //if (e.X<1)
                //{
                //    int cardid1 = this.treeView1.SelectedNode.Parent.Index;
                //    int channelno1 = this.treeView1.SelectedNode.Index;
                //    if (GlobalProject.ACQUnitList.ACQUnitList[Mdl_Class.acqId].ACQCardList.ACQCards[cardid1].ChannelList.Channels[channelno1].SensorType == "0")
                //    {
                //        this.contextMenuStrip1.Items[0].Visible = true;
                //        this.contextMenuStrip1.Items[6].Visible = true;
                //        //this.contextMenuStrip1.Items[4].Visible = true;
                //        //this.contextMenuStrip1.Items[5].Visible = true;
                //    }
                //    else
                //    {
                //        this.treeView1.ContextMenuStrip = this.contextMenuStrip3;
                //    }
                //}
                //else
                //{
                //    this.contextMenuStrip1.Items[0].Visible = false;
                //    this.contextMenuStrip1.Items[6].Visible = false;
                //    //this.contextMenuStrip1.Items[4].Visible = false;
                //    //this.contextMenuStrip1.Items[5].Visible = false;
                //}
                //if (treeView1.SelectedNode.Parent == null || treeView1.SelectedNode.Nodes.Count > 0)
                //{
                //    this.contextMenuStrip1.Items[1].Visible = false;
                //    this.contextMenuStrip1.Items[2].Visible = false;
                //    this.contextMenuStrip1.Items[3].Visible = false;

                //    this.contextMenuStrip1.Items[7].Visible = false;
                //    this.contextMenuStrip1.Items[8].Visible = false;
                //    this.contextMenuStrip1.Items[9].Visible = false;
                //    this.contextMenuStrip1.Items[10].Visible = false;
                //    this.contextMenuStrip1.Items[11].Visible = false;

                //}
                //else
                //{
                //    //this.contextMenuStrip1.Items[1].Visible = true;
                //    //this.contextMenuStrip1.Items[2].Visible = true;
                //    //this.contextMenuStrip1.Items[3].Visible = true;
                //    //this.contextMenuStrip1.Items[4].Visible = true;

                //    //this.contextMenuStrip1.Items[6].Visible = true;
                //    //this.contextMenuStrip1.Items[7].Visible = true;
                //    //this.contextMenuStrip1.Items[8].Visible = true;
                //    //this.contextMenuStrip1.Items[9].Visible = true;
                //    //this.contextMenuStrip1.Items[10].Visible = true;
                //    //this.contextMenuStrip1.Items[11].Visible = true;


                //}


            }
        }

        private void 转换为dBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (chart_Type != 1)
            {
                this.chart1.Series.Clear();
                this.dB_Item.Enabled = false;
                this.Apm_Item.Enabled = true;

                ReflashCharts("单位转换");
                ReflashRMS();
            }
            else 
            {
                MessageBox.Show("对数坐标无法转换","提示");
            }
        }

        private void 积分ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (chart_Type != 1&& !nowUnit.Contains("dB("))
            {
            this.chart1.Series.Clear();
                Now_Type = "积分";
                ReflashCharts("积分");
            ReflashRMS();
            }
            else if (nowUnit.Contains("dB(")) 
            {
                MessageBox.Show("单位"+ nowUnit + "无法积分", "提示");
            }
            else
            {
                MessageBox.Show("对数坐标无法积分", "提示");
            }
        }


        string Now_Type="";
        private void Apm_Item_Click(object sender, EventArgs e)
        {
            if (chart_Type != 1)
            {
            this.chart1.Series.Clear();
            this.dB_Item.Enabled = true;
            this.Apm_Item.Enabled = false;
                Now_Type = "单位转换";
            ReflashCharts("单位转换");
            ReflashRMS();
            }
            else
            {
                MessageBox.Show("对数坐标无法转换", "提示");
            }
        }


        public void ReflashCharts(string type)
        {
            try
            {
                isaddyb = false;
      
                yb1.Points.Clear();
                yb2.Points.Clear();
                IsDisplayDoubleCursorValue(false);
                for (int i = 0; i < this.chart1.Series.Count; i++)
                {
                    if (this.chart1.Series[i].Name.Contains("游标"))
                    {
                        this.chart1.Series[i].Points.Clear();
                        this.chart1.Series.Remove(this.chart1.Series[i]);
                        i--;
                    }
                }
                if (this.chart1.Series.Contains(yb1))
                {
                    this.chart1.Series.Remove(yb1);
                }
                if (this.chart1.Series.Contains(yb2))
                {
                    this.chart1.Series.Remove(yb2);
                }
                this.chart1.Series.Clear();

                this.chart1.ChartAreas[0].CursorX.LineColor = Color.Black;
                this.chart1.ChartAreas[0].CursorX.LineWidth = 1;
                if (nowUnit == "加速度(m/s2)" || nowUnit == "加速度(dB)" || nowUnit == "速度(mm/s)" || nowUnit == "速度(dB)")
            {
                foreach (var item in testNameToSeries.Keys)
                {
                    switch (type)
                    {
                        case "单位转换":
                            DataFormat dataformat = new DataFormat();
                            dataformat.Unit = nowUnit;
                            dataformat.X = testNameToData[item].x_value_all.ToArray();
                            dataformat.Y = testNameToData[item].y_value_all.ToArray();
                            DataFormat dt_new = unitconverter.UnitDispatcher(dataformat);
                                testNameToSeries[item].Points.Clear();
                            testNameToSeries[item].Points.DataBindXY(testNameToData[item].x_value_all, dt_new.Y);

                            //testNameToData[item].x_value = dt_new.X.ToList();
                            //testNameToData[item].x_value_all = dt_new.X.ToList();
                            testNameToData[item].y_value = dt_new.Y.ToList();
                            testNameToData[item].y_value_all = dt_new.Y.ToList();

                            //this.chart1.ChartAreas[0].AxisX.Title = "单位(HZ)";
                            this.chart1.ChartAreas[0].AxisY.Title = dt_new.Unit;
                                //this.chart1.ChartAreas[0].AxisX.Maximum = dt_new.Y.ToArray().Max() * 1.5;
                            nowUnit = dt_new.Unit;
                                this.chart1.Series.Add(testNameToSeries[item]);
                                this.chart1.ChartAreas[0].AxisY.Maximum = Math.Round(dt_new.Y.ToArray().Max()* 1.3,3);
                                this.chart1.ChartAreas[0].AxisY.Minimum = Math.Round(dt_new.Y.ToArray().Min()/ 1.3,3);

                                break;
                        case "积分":

                            
                            DataFormat dataformat2 = new DataFormat();
                            dataformat2.Unit = nowUnit;
                            dataformat2.X = testNameToData[item].x_value_all.ToArray();
                            dataformat2.Y = testNameToData[item].y_value_all.ToArray();
                            DataFormat dt_new2 = unitconverter.IntegrationDispatcher(dataformat2);
                                testNameToSeries[item].Points.Clear();
                                testNameToSeries[item].Points.DataBindXY(testNameToData[item].x_value_all, dt_new2.Y);
                            //testNameToData[item].x_value = dt_new2.X.ToList();
                            //testNameToData[item].x_value_all = dt_new2.X.ToList();
                            testNameToData[item].y_value = dt_new2.Y.ToList();
                            testNameToData[item].y_value_all = dt_new2.Y.ToList();

                            //this.chart1.ChartAreas[0].AxisX.Title = "单位(HZ)";
                            this.chart1.ChartAreas[0].AxisY.Title = dt_new2.Unit;
                            nowUnit = dt_new2.Unit;
                                this.chart1.Series.Add(testNameToSeries[item]);

                                this.chart1.ChartAreas[0].AxisY.Maximum = Math.Round(dt_new2.Y.ToArray().Max() * 1.3,3);
                                this.chart1.ChartAreas[0].AxisY.Minimum = Math.Round(dt_new2.Y.ToArray().Min()/ 1.3,3);
                                break;
                        default:
                            break;
                    }
                }
            }
            else
            {
                MessageBox.Show("不支持该格式的数据类型转换", "提示");
            }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message,"提示");
            }

        }

        private void 导出数据TXTToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {


                if (testNameToData.Count > 0)
                {
                    //string channelid = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                    //string testid = this.dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                    //string channelname = this.dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                    //Cls_Data clsdatalist = operdata.GetDataByChannelid(testid, Convert.ToByte(channelid));
                    //if (clsdatalist != null)
                    //{
                        System.Windows.Forms.SaveFileDialog objSave = new System.Windows.Forms.SaveFileDialog();
                        objSave.Filter = "(*.txt)|*.txt|" + "(*.*)|*.*";
                        if (objSave.ShowDialog() == DialogResult.OK)
                        {
                            string filename = objSave.FileName;
                        using (StreamWriter objWriter = new StreamWriter(filename, false, System.Text.Encoding.UTF8))
                        {
                            //    string result = "";

                            //    if (channelname.Contains("频谱数据"))
                            //    {
                            //        result = String.Join(",", clsdatalist.AccelerationFFTAmpDB.ToArray());
                            //        objWriter.WriteLine(result);
                            //    }
                            //    else if (channelname.Contains("1/3倍频程"))
                            //    {
                            //        result = String.Join(",", clsdatalist._1_3octavedB.ToArray());
                            //        objWriter.WriteLine(result);
                            //    }
                            //    else if (channelname.Contains("时域数据"))
                            //    {
                            //        result = String.Join(",", clsdatalist.AccelerationTimewave.ToArray());
                            //        objWriter.WriteLine(result);
                            //    }
                            //    else if (channelname.Contains("原始数据"))
                            //    {
                            //        string nowsectionName = this.dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
                            //        string nowtestName = this.dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
                            //        string nowchannel = this.dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
                            //        string filePath = Mdl_Class.FilePath.Split('.')[0] + "\\" + nowsectionName + "\\" + nowtestName;
                            //        string fileName = "原始数据_" + nowchannel + ".ini";
                            //        List<float> displayvalue = opertext.ReadOldData(filePath + "\\" + fileName);
                            //        result = String.Join(",", displayvalue.ToArray());
                            //        objWriter.WriteLine(result);
                            //    }
                            //    else
                            //    {
                            //        MessageBox.Show("不支持该数据类型", "提示");
                            //        return;
                            //    }

                            int importcount = 0;
                            string importtitle = "";
                            string zz = "";
                            foreach (var item in testNameToData.Keys)
                            {
                                importcount = testNameToData[item].x_value_all.Count;
                                importtitle = importtitle + item.PadRight(50, ' ');

                                //string s2 = String.Format("{0,13}", s);
                                zz = zz + ("X" + "  " + this.chart1.ChartAreas[0].AxisX.Title).PadRight(25, ' ') + ("Y" + "  " + this.chart1.ChartAreas[0].AxisY.Title).PadRight(25, ' ');

                            }
                            objWriter.WriteLine(importtitle);
                            objWriter.WriteLine( zz);
                                for (int i = 0; i < importcount; i++)
                                {
                                    string a = "";
                                    foreach (var item in testNameToData.Keys)
                                {
                                    if (Nowchannlname.Contains("倍频程"))
                                    {
                                        a = a + (axis_txt[i].ToString()).PadRight(30, ' ') + (testNameToData[item].y_value_all[i].ToString()).PadRight(30, ' ') + "      ";
                                    }
                                    else
                                    {
                                        a = a + (testNameToData[item].x_value_all[i].ToString()).PadRight(30, ' ') + (testNameToData[item].y_value_all[i].ToString()).PadRight(30, ' ') + "      ";
                                        //a = a + String.Format("{0,30}",(testNameToData[item].x_value_all[i].ToString())) + String.Format("{0,30}", (testNameToData[item].y_value_all[i].ToString()));
                                    }
                                }
                                    objWriter.WriteLine( a);
                                }

                            }
                            MessageBox.Show("导出成功", "提示");
                        }
                    //}
                    //else
                    //{
                    //    MessageBox.Show("未查询可导出的数据", "提示");
                    //}
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("导出异常，异常信息：" + ex.Message, "提示");
            }
        }

        private void chart1_Click(object sender, EventArgs e)
        {

        }

        private void x轴范围设定ToolStripMenuItem_Click(object sender, EventArgs e)
        {


        }

        private void button2_Click(object sender, EventArgs e)
        {
  
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            ReflashDoubleCursorPosition();
        }

        public void ReflashDoubleCursorPosition()
        {
            try
            {

         
            if (this.txt_left_value.Text.Trim() == "" && this.txt_right_value.Text.Trim() == "")
            {
                MessageBox.Show("范围值不能为空", "提示");
                return;
            }
            else
            {
                try
                {
               
                    double minvalue = Convert.ToDouble(this.txt_left_value.Text.Trim());
                    double maxvalue = Convert.ToDouble(this.txt_right_value.Text.Trim());
                    if (maxvalue< min_data) 
                    {
                        MessageBox.Show("左边游标值要小于右边游标值", "提示");
                    }
                    max_data = maxvalue;
                    min_data = minvalue;
                    
                    //Dispaly_SelectChart(minvalue, maxvalue);
                    if (min_data < 0 || max_data > x_value.ToArray().Max() + 1)
                    {
                        MessageBox.Show("范围超出", "提示");
                        return;
                    }
                    double cz = 0;
                    double Curx_value = 0;
                    if (chart_Type == 1)
                    {
                        this.chart1.ChartAreas[0].CursorX.Position= Math.Log10(min_data);
                        if ( this.chart1.ChartAreas[0].CursorX.Position + cz< this.chart1.ChartAreas[0].AxisX.Maximum)
                        {



                            cz = Math.Log10(maxvalue)- Math.Log10(minvalue);

                            Curx_value = Math.Pow(10, this.chart1.ChartAreas[0].CursorX.Position);

                            //yb1.Points[0].XValue = Math.Pow(10, minvalue);
                            //yb1.Points[1].XValue = Math.Pow(10, minvalue);
                            //yb2.Points[0].XValue = Math.Pow(10, maxvalue);
                            //yb2.Points[1].XValue = Math.Pow(10, maxvalue);
                            yb1.Points[0].XValue = minvalue;
                            yb1.Points[1].XValue = minvalue;
                            yb2.Points[0].XValue =maxvalue;
                            yb2.Points[1].XValue = maxvalue;
                        }
                    }
                    else
                    {
                        if (yb2.Points[0].XValue + cz < this.chart1.ChartAreas[0].AxisX.Maximum)
                        {
                            this.chart1.ChartAreas[0].CursorX.Position = min_data;
                            cz = yb2.Points[0].XValue - yb1.Points[0].XValue;
                            Curx_value = this.chart1.ChartAreas[0].CursorX.Position;

                            yb1.Points[0].XValue = min_data;
                            yb1.Points[1].XValue = min_data;

                            yb2.Points[0].XValue = max_data;
                            yb2.Points[1].XValue = max_data;
                        }
                    }
                    ismove = true;
                    movSeries = yb1;
                    DispalyCursorPosition(true);

                    ismove = false;
                }
                catch (Exception ex)
                {

               
                    MessageBox.Show("输入有误,只能输入小数或者整数", "提示");
                    return;
                }
            }
            }
            catch (Exception ex)
            {

                MessageBox.Show("显示异常,请重新输入", "提示");
            }
        }

        private void Button2_Click_1(object sender, EventArgs e)
        {
            YorN1();
        }

        private void Bt_dyb_Click(object sender, EventArgs e)
        {
            if (this.txt_dyb.Text.Trim()!="")
            {
                try
                {
                    double aa = Convert.ToDouble(this.txt_dyb.Text.Trim());
                    if (aa > x_value.ToArray().Max())
                    {
                        MessageBox.Show("范围超出", "提示");
                        return;
                    }
                    if (chart_Type == 1)
                    {
                        this.chart1.ChartAreas[0].CursorX.Position = Math.Log10(aa);
                    }
                    else
                    {
                        this.chart1.ChartAreas[0].CursorX.Position = aa;
                    }
                    DispalyCursorPosition(false);

                }
                catch (Exception)
                {
                    MessageBox.Show("输入有误,只能输入小数或者整数", "提示");
                    return;
                }
            }
        }
        Export_Data export_Data = new Export_Data();
        private void 导出数据ExeclToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                System.Windows.Forms.SaveFileDialog saveDia = new SaveFileDialog();
                saveDia.Filter = "Excel|*.xlsx";
                saveDia.Title = "导出为Excel文件";
                string path = "";
                if (saveDia.ShowDialog() == System.Windows.Forms.DialogResult.OK
               && !string.Empty.Equals(saveDia.FileName))
                {
                    path = saveDia.FileName;

                    DataTable StockTable2 = new DataTable();

         

                    int importcount = 0;
                    string importtitle = "";
                    string zz = "";
                    foreach (var item in testNameToData.Keys)
                    {
                        importcount = testNameToData[item].x_value_all.Count;
                        importtitle = importtitle + item;

                        //string s2 = String.Format("{0,13}", s);
                        DataColumn dc1 = new DataColumn(item, typeof(string));
                        DataColumn dc2= new DataColumn("", typeof(string));
                        StockTable2.Columns.Add(dc1);
                        StockTable2.Columns.Add(dc2);

                        //zz = zz + ("X" + "  " + this.chart1.ChartAreas[0].AxisX.Title).PadRight(25, ' ') + ("Y" + "  " + this.chart1.ChartAreas[0].AxisY.Title).PadRight(25, ' ');

                    }
                    int colindex = 0;
                    DataRow dr = StockTable2.NewRow();
                    foreach (var item in testNameToData.Keys)
                    {
                        //zz = zz + ("X" + "  " + this.chart1.ChartAreas[0].AxisX.Title).PadRight(25, ' ') + ("Y" + "  " + this.chart1.ChartAreas[0].AxisY.Title).PadRight(25, ' ');
           
                        dr[colindex] = "X" + "  " + this.chart1.ChartAreas[0].AxisX.Title;
                        colindex++;
                        dr[colindex] = "Y" + "  " + this.chart1.ChartAreas[0].AxisY.Title;
                        colindex++;

                    
                    }

                    StockTable2.Rows.Add(dr);
                    //objWriter.WriteLine(importtitle);
                    //objWriter.WriteLine(zz);


                    for (int i = 0; i < importcount; i++)
                    {
                        string a = "";
                        DataRow dr2 = StockTable2.NewRow();
                        int colindex2 = 0;
                        foreach (var item in testNameToData.Keys)
                        {
                            if (Nowchannlname.Contains("倍频程"))
                            {
                                //a = a + (axis_txt[i].ToString()).PadRight(30, ' ') + (testNameToData[item].y_value_all[i].ToString()).PadRight(30, ' ') + "      ";


                                dr2[colindex2] = axis_txt[i].ToString();
                                colindex2++;
                                dr2[colindex2] = testNameToData[item].y_value_all[i].ToString();
                                colindex2++;
                            }
                            else
                            {
                                //a = a + (testNameToData[item].x_value_all[i].ToString()).PadRight(30, ' ') + (testNameToData[item].y_value_all[i].ToString()).PadRight(30, ' ') + "      ";



                                dr2[colindex2] = testNameToData[item].x_value_all[i].ToString();
                                colindex2++;
                                dr2[colindex2] = testNameToData[item].y_value_all[i].ToString();
                                colindex2++;
                            }
                        }
                        StockTable2.Rows.Add(dr2);
                    }

                    export_Data.DataTabletoExcel(StockTable2, path);
                    //MessageBox.Show("导出成功","提示");
                }
        
                //StockTable2.Columns.Add(dc1);
                //StockTable2.Columns.Add(dc2);
                //StockTable2.Columns.Add(dc3);

                //datatable_new.Rows.Add();
                //export_Data.DataTabletoExcel(dt1, path);
            
            }
            catch (Exception ex)
            {

                MessageBox.Show("导出异常：" + ex.Message, "导出异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void 添加限值线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.panel_xz.Visible = true;
            ReadXZZ();
        }
    
        private void Bt_xzx_add_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.chart1.Series.Contains(xzx)) {
                    this.chart1.Series.Remove(xzx);
                }
                xzx.Points.Clear();
                for (int i = 0; i < this.dataGridView2.ColumnCount; i++)
                {
                    xzx.Points.AddXY(i, Convert.ToDouble(this.dataGridView2.Rows[0].Cells[i].Value));
                    xzzValue[i] = Convert.ToDouble(this.dataGridView2.Rows[0].Cells[i].Value);
                }
     
                xzx.ChartType = SeriesChartType.Line;
                xzx.Color = Color.Black;
                xzx.BorderWidth = 2;
                xzx.Name = "xzx";
               this.chart1.Series.Add(xzx);
                WriteXZZ();
                this.panel_xz.Visible = false;
            }
            catch (Exception ex)
            {

                MessageBox.Show("设置出错，出错原因:" + ex.Message, "提示");
            }
        }

        private void Bt_xzx_del_Click(object sender, EventArgs e)
        {
            //if (this.chart1.Series.Contains(xzx))
            //{
            //    this.chart1.Series.Remove(xzx);
            //}
            this.panel_xz.Visible = false;
        }

        private void 删除限值线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.chart1.Series.Contains(xzx))
            {
                this.chart1.Series.Remove(xzx);
            }
        }


        public void ReadXZZ()
        {
            string filepathname = Application.StartupPath + "\\限制值.txt";
            string readvalue = opertext.ReadTXT(filepathname);
            if (readvalue!="")
            {
                string[] valuelist = readvalue.Split(',');
                for (int i = 0; i < valuelist.Length; i++)
                {
                    xzzValue[i] = Convert.ToDouble(valuelist[i]);
                }
            }
            this.dataGridView2.Rows.Clear();
            this.dataGridView2.Rows.Add();
            for (int i = 0; i < this.dataGridView2.ColumnCount; i++)
            {
                this.dataGridView2.Columns[i].HeaderText = axis_txt[i].ToString();
                this.dataGridView2.Columns[i].Width = 50;
                this.dataGridView2.Rows[0].Cells[i].Value = xzzValue[i].ToString();
            }

        }

        public void WriteXZZ()
        {
            string filepathname = Application.StartupPath;
            string writxtPath = "限制值.txt";
            string writxt = "";
            for (int i = 0; i < xzzValue.Length; i++)
            {
                if (i == 0)
                {
                    writxt = writxt + xzzValue[i].ToString();

                }
                else
                {
                    writxt = writxt +","+ xzzValue[i].ToString();
                }
            }
            opertext.WriteTXT(filepathname, writxtPath, writxt);
        }
    }


    public class testTodata
    {
       public  List<double> x_value = null;
       public  List<double> y_value = null;
       public  List<double> x_value_all = null;
       public  List<double> y_value_all = null;
    }

    public class CursorValue
    {
        public double[] Y_values;
        public string Max_Value;
        public double Min_Value;
        public double Avg_Value;

        public double leftValue;
        public double rightValue;
    }

}

